//引入公共函数
var util = require('../../utils/util');

//存放页面实例的变量
var that;

//请求域名的URL
var requestDomain = util.config('requestDomain');

Page({
    data: {
        orderDate: '',
        withdrawCashId: '',
        withdrawCashDate: '-',
        withdrawCashName: '-',
        withdrawCashTotal: '-',
        orderList: []
    },
    onLoad: function(params) {

        that = this;

        let {date, withdrawCashId} = params;

        that.setData({
            orderDate: date,
            withdrawCashId: withdrawCashId
        });

        that.getWithdrawCashDetail(withdrawCashId);

    },

    getWithdrawCashDetail: function(withdrawCashId) {

        wx.showNavigationBarLoading();

        let {orderDate, orderList} = that.data;

        wx.request({
          url: requestDomain + 'withdrawCash/getDetail',

          data: {

            storeId: util.globalData('storeId'),

            _3rdSession: util.globalData('_3rdSession'),

            withdrawCashId: withdrawCashId,

            orderDate: orderDate

          },

          method: 'POST',

          success: function(result){

            wx.hideNavigationBarLoading();
              
            if(util.checkSuccess(result) && result.data && result.data.data) {
                
                let {userName, withdrawCashOrderList, withdrawCashLogs, withdrawCashTotal} = result.data.data;

                for(let orderIndexA in withdrawCashLogs.withdrawCashOrderList) {
                    for(let orderIndexB in withdrawCashOrderList) {
                        if(withdrawCashLogs.withdrawCashOrderList[orderIndexA] == withdrawCashOrderList[orderIndexB].orderId) {
                            orderList.push({
                                orderId: withdrawCashOrderList[orderIndexB].orderId,
                                orderDate: util.formatTime(new Date(withdrawCashOrderList[orderIndexB].timestamp * 1000)),
                                withdrawCashStatus: withdrawCashOrderList[orderIndexB].storeReceiveMoney ? 1 : (withdrawCashOrderList[orderIndexB].withdrawCashId != '' ? 2 : 3)
                            });
                        }
                    }
                }

                console.log(userName, withdrawCashOrderList, withdrawCashLogs);

                let withdrawCashExpense = withdrawCashTotal > 99 ? ((withdrawCashTotal * 0.02).toFixed(2) / 100).toFixed(2) : (0).toFixed(2);

                that.setData({

                    orderList: orderList,
                    withdrawCashName: userName,
                    withdrawCashDate: util.formatTime(new Date(withdrawCashLogs.withdrawCashTimestamp)),
                    withdrawCashTotal: withdrawCashTotal / 100,
                    withdrawCashExpense: withdrawCashExpense,
                    withdrawCashResultTotal: ((withdrawCashTotal - withdrawCashTotal * 0.02).toFixed(2) / 100).toFixed(2)

                });

            }
            else {

                util.showError('查询失败', '查询提现详情时发生错误' + result.data.errMsg, result.data.errCode);

            }
          },

          fail: function(err) {
            console.log(err);
            wx.hideNavigationBarLoading();
            util.showError('查询失败', '发起提现详情查询请求失败');
          }
        });
    },
    goOrderDetail: function(e) {
        let orderId = e.currentTarget.dataset.orderid;
        wx.navigateTo({
            url: '/pages/orderDetail/orderDetail?orderId=' + orderId,
        });
    }
});